import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter/foundation.dart' show SynchronousFuture;

import 'key.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      onGenerateTitle: (context) {
        return DemoLocalizations.of(context).taskTitle;
      },
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: FreeLocalizations(
        key: freeLocalizationStateKey,
        child: MyHomePage(),
      ),
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        DemoLocalizationsDelegate.delegate,
        
      ],
      supportedLocales: [
        Locale('zh', 'CH'),
        Locale('en', 'US'),
      ],
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  bool flag = true;
  void changeLocale() {
    if (flag) {
      freeLocalizationStateKey.currentState.changeLocale(Locale('zh', "CH"));
    } else {
      freeLocalizationStateKey.currentState.changeLocale(Locale('en', "US"));
    }
    flag = !flag;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(DemoLocalizations.of(context).titleBarTitle),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              DemoLocalizations.of(context).clickTop,
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: changeLocale,
        tooltip: DemoLocalizations.of(context).inc,
        child: Icon(Icons.add),
      ),
    );
  }
}

class DemoLocalizations {
  final Locale locale;

  DemoLocalizations(this.locale);

  get _localizedValues => {
        'en': {
          'task title': 'Flutter Demo',
          'titlebar title': 'Flutter Demo Home Page',
          'click tip': 'You have pushed the button this many times:',
          'inc': 'Increment'
        },
        'zh': {
          'task title': 'Flutter 示例',
          'titlebar title': 'Flutter 示例主页面',
          'click tip': '你一共点击了这么多次按钮：',
          'inc': '增加'
        }
      };

  get taskTitle {
    return _localizedValues[locale.languageCode]['task title'];
  }

  get titleBarTitle {
    return _localizedValues[locale.languageCode]['titlebar title'];
  }

  get clickTop {
    return _localizedValues[locale.languageCode]['click tip'];
  }

  get inc {
    return _localizedValues[locale.languageCode]['inc'];
  }

  factory DemoLocalizations.of(BuildContext context) {
    return Localizations.of(context, DemoLocalizations);
  }
}

class DemoLocalizationsDelegate
    extends LocalizationsDelegate<DemoLocalizations> {
  DemoLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) {
    return ['en', 'zh'].contains(locale.languageCode);
  }

  @override
  load(Locale locale) {
    return SynchronousFuture<DemoLocalizations>(DemoLocalizations(locale));
  }

  @override
  bool shouldReload(LocalizationsDelegate<DemoLocalizations> old) {
    return false;
  }

  static final delegate = DemoLocalizationsDelegate();
}

